package com.duan.laugh.msg.controller;

import com.duan.laugh.msg.api.pojo.entity.Msg;
import com.duan.laugh.msg.api.constans.MsgConstants;
import com.duan.laugh.msg.service.MsgService;
import lombok.AllArgsConstructor;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @author duanjw
 */
@AllArgsConstructor
@RestController
@RequestMapping("msg")
public class MsgController {
    private final MsgService msgService;
    private final AmqpTemplate amqpTemplate;

    /**
     * 给指定用户推送消息
     *
     * @param rec
     */
    @PostMapping("/to/{rec}")
    public void sendMsgToUser(@PathVariable String rec, @RequestBody Msg msg) {
        msg.setRec(rec);
        msgService.sendMsgToUser(msg);

    }

    /**
     * 给所有用户推送消息
     *
     * @param msg
     */
    @PostMapping("/all")
    public void sendMsgToAllUser(@RequestBody Msg msg) {
        msgService.sendMsgToAllUser(msg);
    }

    /**
     * 给指定用户推送消息，先推到MQ，再监听MQ消息并推到前端
     */
    @PostMapping("mq/to/{rec}")
    public void sendMsgToUserByMq(@PathVariable String rec, @RequestBody Msg msg) {
        msg.setRec(rec);
        amqpTemplate.convertAndSend(MsgConstants.MQ_QUEUE_SYS_MSG, msg);
    }
}
